<!--
 * Copyright 2013 The Chromium Authors. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<html>
  <head>
    <script type="text/javascript">
      // A guest that requests geolocation.
      // Notifies the embedder about the result of the request (success/fail)
      // via post message. Note that the embedder has to initiate a postMessage
      // first so that guest has a reference to the embedder's window.

      // The window reference of the embedder to send post message reply.
      var embedderWindowChannel = null;

      // Url of the iframe, if set via postMessage, then the guest also loads an
      // <iframe>.
      var iframeURL;

      var g_testName = 'uninitialized';
      var expectedTotalCallbackCount;
      var successCallbackCount;
      var totalCallbackCount;
      var lastLat = -181;
      var lastLng = -181;
      var lastLatLng = [];

      var maybeNotifyEmbedder = function() {
        console.log('maybeNotifyEmbedder' +
            ', expectedTotalCallbackCount: ' + expectedTotalCallbackCount +
            ', successCallbackCount: ' + successCallbackCount +
            ', totalCallbackCount: ' + totalCallbackCount);
        if (expectedTotalCallbackCount == totalCallbackCount) {
          var status = (expectedTotalCallbackCount == successCallbackCount) ?
              'access-granted' : 'access-denied';
          var responseArray = [g_testName, status];
          if (lastLat != -181) {
            responseArray.push(lastLat);
            responseArray.push(lastLng);
          }
          notifyEmbedder(responseArray);
        }
      };

      var notifyEmbedder = function(msg_array) {
        embedderWindowChannel.postMessage(JSON.stringify(msg_array), '*');
      };

      var startTest = function() {
        totalCallbackCount = 0;
        successCallbackCount = 0;
        expectedTotalCallbackCount = 1;
        if (iframeURL) {
          // The iframe will initiate another geolocation request.
          ++expectedTotalCallbackCount;
        }

        window.console.log('Call getCurrentPosition');
        navigator.geolocation.getCurrentPosition(onGeolocationSuccess,
                                                 onGeolocationFailure);
      };


      var onGeolocationSuccess = function(position) {
        ++successCallbackCount;
        ++totalCallbackCount;
        console.log('onGeolocationSuccess, successCallbackCount: ' +
                    successCallbackCount +
                    'totalCallbackCount: ' + totalCallbackCount);

        lastLat = position.coords.latitude;
        lastLng = position.coords.longitude;
        maybeNotifyEmbedder();

        if (iframeURL) {
          document.querySelector('#iframe-container').innerHTML =
              '<iframe src="' + iframeURL + '" ' +
              '  width="100" height="100"></iframe>';
          iframeURL = '';
        }
      };

      var onGeolocationFailure = function(err) {
        ++totalCallbackCount;
        console.log('onGeolocationFailure, totalCallbackCount: ' +
                    totalCallbackCount);
        maybeNotifyEmbedder();
      };

      // Callback entry points for the <iframe>.
      window['onIframeGeolocationSuccess'] = onGeolocationSuccess;
      window['onIframeGeolocationFailure'] = onGeolocationFailure;

      var onPostMessageReceived = function(e) {
        window.console.log('guest.onPostMessageReceived');
        var data = JSON.parse(e.data);
        if (data[0] == 'check-geolocation-permission') {
          g_testName = data[1];
          iframeURL = data[2];
          embedderWindowChannel = e.source;
          // Start the test once we have |embedderWindowChannel|.
          startTest();
        }
      };
      window.addEventListener('message', onPostMessageReceived, false);
    </script>
  </head>
  <body>
    <div>This is a guest that requests geolocation.</div>
    <div id="iframe-container"></div>
    <script>
      console.log('Guest loaded');
    </script>
  </body>
</html>
